package org.aphelie.torg.ruler.internal.core;

import java.net.URL;
import java.util.ArrayList;

import org.aphelie.torg.ruler.core.character.CharacterRegistry;
import org.aphelie.torg.ruler.core.character.ICharacterModel;
import org.aphelie.torg.ruler.core.character.ICharacterModelBase;
import org.aphelie.torg.ruler.core.character.IFragmentModel;
import org.eclipse.core.resources.IProject;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.HostSpecification;


public class TGRManager {

    /* TODO
    public static IFragmentModel[] findFragmentsFor(ICharacterModelBase model) {
        ArrayList result = new ArrayList();
        BundleDescription desc = getBundleDescription(model);
        if (desc != null) {
            BundleDescription[] fragments = desc.getFragments();
            for (int i = 0; i < fragments.length; i++) {
                ICharacterModelBase candidate = CharacterRegistry.findModel(fragments[i]);
                if (candidate instanceof IFragmentModel) {
                    result.add(candidate);
                }
            }
        }
        return (IFragmentModel[]) result.toArray(new IFragmentModel[result.size()]);
    }
    */

    public static ICharacterModel findHostFor(IFragmentModel fragment) {
        BundleDescription desc = getBundleDescription(fragment);
        if (desc != null) {
            HostSpecification spec = desc.getHost();
            if (spec != null) {
                ICharacterModelBase host = CharacterRegistry.findModel(spec.getName());
                if (host instanceof ICharacterModel)
                    return (ICharacterModel) host;
            }
        }
        return null;
    }

    private static BundleDescription getBundleDescription(ICharacterModelBase model) {
        BundleDescription desc = model.getBundleDescription();

        if (desc == null && model.getUnderlyingResource() != null) {
            // the model may be an editor model. 
            // editor models don't carry a bundle description
            // get the core model counterpart.
            IProject project = model.getUnderlyingResource().getProject();
            ICharacterModelBase coreModel = CharacterRegistry.findModel(project);
            if (coreModel != null)
                desc = coreModel.getBundleDescription();
        }
        return desc;
    }

    public static URL[] getNLLookupLocations(ICharacterModelBase model) {
        ArrayList urls = new ArrayList();
        addNLLocation(model, urls);
        if (model instanceof ICharacterModel) {
            /* TODO
            IFragmentModel[] fragments = findFragmentsFor(model);
            for (int i = 0; i < fragments.length; i++) {
                addNLLocation(fragments[i], urls);
            }
            */
        } else if (model instanceof IFragmentModel) {
            ICharacterModel host = findHostFor((IFragmentModel) model);
            if (host != null)
                addNLLocation(host, urls);
        }
        return (URL[]) urls.toArray(new URL[urls.size()]);
    }

    private static void addNLLocation(ICharacterModelBase model, ArrayList urls) {
        URL location = model.getNLLookupLocation();
        if (location != null)
            urls.add(location);
    }

    /* TODO
    public static String getBundleLocalization(ICharacterModelBase model) {
        if (model instanceof IBundlePluginModelBase && model.getUnderlyingResource() != null)
            return ((IBundlePluginModelBase) model).getBundleLocalization();

        if (model instanceof ExternalPluginModelBase)
            return ((ExternalPluginModelBase) model).getLocalization();

        return Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
    }
    */
    
}
